<?php

namespace app\merchant\job;

use app\common\model\CardFans;
use app\common\model\CardGeneral;
use app\common\model\CardLeaduserLins;
use app\common\model\CardMember;
use app\common\model\LoginUsers;
use app\common\model\Merchant;
use app\common\model\Momerchant;
use app\common\model\OpenTemplate;
use app\common\model\System;
use app\common\model\WxOpen;
use think\Queue;
use think\queue\Job;
use think\Session;
use Wechat\Wxobjopen;

class Leaduser
{

    public function fire(Job $job, $data)
    {
//        switch ($data['type']){
//            //批量同步粉丝
//            case '1':
//                $isJobDone = $this->task_tpl_card($data);
//                break;
//            case '2':
//                $isJobDone = $this->task_syn_fans($data);
//                break;
////            default:
////                return "9003";
//        }
        $isJobDone = $this->task_lead_user($data);
        $job->delete();

        if ($isJobDone) {
            //如果任务执行成功， 记得删除任务
            $job->delete();
            return '完成';
        } else {
            if ($job->attempts() > 3) {
                //通过这个方法可以检查这个任务已经重试了几次了
                print("<warn>任务已完成3次!" . "</warn>\n");
                $job->delete();
                // 也可以重新发布这个任务
                //print("<info>Hello Jobmach will be availabe again after 2s."."</info>\n");
                //$job->release(2); //$delay为延迟时间，表示该任务延迟2秒后再执行
            }
        }
    }



    /**
     * 根据消息中的数据进行实际的业务处理
     * @param array|mixed $data 发布任务时自定义的数据
     * @return boolean                 任务执行的结果
     */
    //批量同步粉丝
    public function task_lead_user($data)
    {

//        return $data;
        $CardLeaduser = new CardLeaduserLins();
        //初始化微信类
//        foreach ($data as $k => $v) {
//            $sinfo=$CardLeaduser->where(array('openid'=>$v['openid']))->find();
//            if($sinfo){
//                $datas[$k]['id'] = $sinfo['id'];
//                $datas[$k]['wx_open_id'] = $data['wx_open_id'];
//                $datas[$k]['gh_id'] = $data['gh_id'];
//                $datas[$k]['statu'] = $v['subscribe'];
//                $datas[$k]['openid'] = $v['openid'];
//                $datas[$k]['nickname'] = $v['nickname'];
//                $datas[$k]['sex'] = $v['sex'];
//                $datas[$k]['city'] = $v['city'];
//                $datas[$k]['head_url'] = $v['headimgurl'];
//                $datas[$k]['groupid'] = $v['groupid'];
//                $datas[$k]['create_time'] = $v['subscribe_time'];
//           }else{
//                $datas2[$k]['wx_open_id'] = $data['wx_open_id'];
//                $datas2[$k]['gh_id'] = $data['gh_id'];
//                $datas2[$k]['statu'] = $v['subscribe'];
//                $datas2[$k]['openid'] = $v['openid'];
//                $datas2[$k]['nickname'] = $v['nickname'];
//                $datas2[$k]['sex'] = $v['sex'];
//                $datas2[$k]['city'] = $v['city'];
//                $datas2[$k]['head_url'] = $v['headimgurl'];
//                $datas2[$k]['groupid'] = $v['groupid'];
//                $datas2[$k]['create_time'] = $v['subscribe_time'];
//            }
//        }
        $CardLeaduser->allowField(true)->saveAll($data);
        return "成功";
    }

}
